﻿using DataModels.Models;
using GenericTools;
using GenericTools.CustomReporting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReportingManagement.Components.Queries
{
    /// <summary>
    /// Interaction logic for QueryCategoryEditor.xaml
    /// </summary>
    public partial class QueryCategoryEditor : Window
    {
        private QueryCategory queryCategory = null;
        private bool isEditing = false;
        private string name;
        private string description;

        public QueryCategoryEditor()
        {
            InitializeComponent();
        }

        public QueryCategoryEditor(QueryCategory category)
        {
            InitializeComponent();

            this.queryCategory = category;
            if (queryCategory == null)
            {
                // Disable every control in the form.
                // Null is not an acceptable value.
                txtName.IsEnabled = false;
                txtDescription.IsEnabled = false;
                btnSave.IsEnabled = false;
                btnReset.IsEnabled = false;
                return;
            }
            else
            {
                // If the query category is the new, then the id should be zero
                isEditing = queryCategory.Id == 0 ? false : true;
                btnReset.IsEnabled = isEditing;
                if (isEditing)
                {
                    name = queryCategory.Name;
                    description = queryCategory.Description;
                }

                this.DataContext = queryCategory;
            }
        }

        public QueryCategory QueryCategory 
        { 
            get 
            { 
                return queryCategory;
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            queryCategory.Name = name;
            queryCategory.Description = description;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            int status = ReportQuerying.STATUS_OK_TO_CONTINUE;
            try
            {
                btnSave.IsEnabled = false;
                if (isEditing) status = ReportQuerying.UpdateQueryCategory(queryCategory);
                else status = ReportQuerying.AddQueryCategory(queryCategory);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, AppConfiguration.ApplicationName, 
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                btnSave.IsEnabled = true;
            }

            if (status == ReportQuerying.STATUS_OK_TO_CONTINUE)
            {
                this.DialogResult = true;
                this.Close();
                return;
            }
            else
            {
                string message = "Unknown error occured that prevents you from saving your changes";
                switch (status)
                {
                    case ReportQuerying.STATUS_NAME_ALREADY_EXISTS:
                        message = "Selected names is already in use. Pick another one";
                        break;

                    case ReportQuerying.STATUS_NAME_IS_MISSING:
                        message = "Category name is required";
                        break;
                }

                MessageBox.Show(message);
                return;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            bool clearChanges = false;
            if (!this.DialogResult.HasValue) clearChanges = true;
            if (this.DialogResult.HasValue && this.DialogResult.Value == false) clearChanges = true;

            if (clearChanges) btnReset_Click(null, null);
        }
    }
}
